﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Threading.Tasks;

namespace WellDone
{
    public sealed partial class TablePickerDialog : Page
    {
        public event EventHandler CloseRequested;
        public bool bUpdated = false;
        public string tableNo = "0";
        public TablePickerDialog()
        {
            this.InitializeComponent();
        }
       
        public async Task<Boolean> init()
        {
            await loadTableLayouts();
            var pane = InputPane.GetForCurrentView();
            pane.Showing += pane_Showing;
            pane.Hiding += pane_Hiding;
            return true;
        }

        private async Task<Boolean> loadTableLayouts()
        {
            // Retrieve table layout records
            Globals.TableLayoutRecords = await Globals.client.GetTableLayoutObjectsAsync(Globals.token, "1=1");
            cmbTableLayout.Items.Clear();
            // Retrieve list of layout records
            foreach (ServiceReferenceOpenRPOS.TableLayout tl in Globals.TableLayoutRecords)
            {
                cmbTableLayout.Items.Add(new ComboBoxItem() { Tag = tl.col_ID, Content = tl.col_Name.Trim() });
            }
            cmbTableLayout.SelectedIndex = 0;
            return true;
        }

        private async void cmbTableLayout_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            await loadTables();
        }

        private async Task<Boolean> loadTables()
        {
            // Retrieve table specification records
            Globals.TableSpecificationRecords = await Globals.client.GetTableSpecificationObjectsAsync(Globals.token, "TableLayoutID=" + cmbTableLayout.SelectedValue.ToString());
            cmbTableNumber.Items.Clear();
            // Retrieve list of table specification
            
            foreach (ServiceReferenceOpenRPOS.TableSpecification ts in Globals.TableSpecificationRecords)
            {
                ComboBoxItem cmbItem = new ComboBoxItem();
                //cmbItem.SetValue("Value", (object)ts.col_TableNumber);
                cmbItem.Content= ts.col_TableNumber;
                cmbTableNumber.Items.Add(cmbItem);                
            }
            cmbTableNumber.SelectedIndex = 0;
            return true;
        }

        private void pane_Showing(InputPane sender, InputPaneVisibilityEventArgs e)
        {
            // Shift the dialog up...
            this.RootPanel.Margin = new Thickness(0, -1 * e.OccludedRect.Y, 0, 0);
        }
        private void pane_Hiding(InputPane sender, InputPaneVisibilityEventArgs e)
        {
            // Restore...
            this.RootPanel.Margin = new Thickness(0, 0, 0, 0);
        }
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            bUpdated = true;
            tableNo = cmbTableNumber.SelectionBoxItem.ToString();
            // Close the dialog
            if (this.CloseRequested != null)
            {
                this.CloseRequested(this, EventArgs.Empty);
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            bUpdated = false;
            // Close the dialog
            if (this.CloseRequested != null)
            {
                this.CloseRequested(this, EventArgs.Empty);
            }
        }
        
    }
}
